package com.ruoyi.flowable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.flowable.domain.WfCategory;
import com.ruoyi.flowable.domain.vo.WfCategoryVo;
import com.ruoyi.flowable.mapper.WfCategoryMapper;
import com.ruoyi.flowable.service.IWfCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 流程分类Service业务层处理
 *
 * @author KonBAI
 * @date 2022-01-15
 */
@RequiredArgsConstructor
@Service
public class WfCategoryServiceImpl implements IWfCategoryService {

    private final WfCategoryMapper baseMapper;

    @Override
    public WfCategoryVo queryById(Long categoryId) {
        return baseMapper.selectOneById(categoryId);
    }

//    @Override
//    public List<WfCategoryVo> queryPageList(WfCategory category) {

    /// /        LambdaQueryWrapper<WfCategory> lqw = buildQueryWrapper(category);
    /// /        Page<WfCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return baseMapper.selectListByCond(category);
//    }
    @Override
    public List<WfCategoryVo> queryList(WfCategory category) {
//        LambdaQueryWrapper<WfCategory> lqw = buildQueryWrapper(category);
        return baseMapper.selectListByCond(category);
    }

//    private LambdaQueryWrapper<WfCategory> buildQueryWrapper(WfCategory category) {
//        LambdaQueryWrapper<WfCategory> lqw = Wrappers.lambdaQuery();
//        lqw.like(StringUtils.isNotBlank(category.getCategoryName()), WfCategory::getCategoryName, category.getCategoryName());
//        lqw.eq(StringUtils.isNotBlank(category.getCode()), WfCategory::getCode, category.getCode());
//        return lqw;
//    }

    @Override
    public int insertCategory(WfCategory categoryBo) {
        WfCategory add = BeanUtil.toBean(categoryBo, WfCategory.class);
        return baseMapper.insert(add);
    }

    @Override
    public int updateCategory(WfCategory categoryBo) {
        WfCategory update = BeanUtil.toBean(categoryBo, WfCategory.class);
        return baseMapper.updateById(update);
    }

    @Override
    public int deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids);
    }

    /**
     * 校验分类编码是否唯一
     *
     * @param category 流程分类
     * @return 结果
     */
    @Override
    public boolean checkCategoryCodeUnique(WfCategory category) {
        int count = baseMapper.countByCond(category);
//        boolean exist = baseMapper.exists(new LambdaQueryWrapper<WfCategory>()
//                .eq(WfCategory::getCode, category.getCode())
//                .ne(ObjectUtil.isNotNull(category.getCategoryId()), WfCategory::getCategoryId, category.getCategoryId()));
        return count <= 0;
    }
}
